from __future__ import division

class Player:
    def __init__(self):
        # Set accumulators to 0
        self.times_hunted = 0
        self.times_slacked = 0
    
    def hunt_choices(self, round_number, current_food, current_rep, m,
                     player_reps):
        # Make my reputation the average
        # i.e: current_rep = mean(player_reps)
        # H is the target for self.times_hunted by the end of the round
        H = int(round(
                _mean(player_reps) * (self.times_hunted + self.times_slacked +
                                      len(player_reps))
            ))
        # Can't hunt more times than there are players
        times_to_hunt = _min(H - self.times_hunted, len(player_reps))
        
        # Set accumulators
        self.times_hunted += times_to_hunt
        self.times_slacked += len(player_reps) - times_to_hunt
        
        # Share out those huntings against opponents with best reputations
        return _allocate(times_to_hunt, 'h', ['s' for p in player_reps],
                         player_reps)
    
    def hunt_outcomes(self, food_earnings):
        pass
    
    def round_end(self, award, m, number_hunters):
        pass
        
    def _mean(l):
        """
        Arithmetic mean
        """
        return sum(l) / len(l)
    
    def _largest_index(l):
        """
        Gets the index part of _largest(l)'s result
        """
        (v, i) = _largest(l)
        return i
    
    def _largest(l):
        """
        Returns a tuple of the largest value in the list and its index
        """
        if len(l) == 1:
            return (l[0], 0)
        else
            (v, i) = _largest(l[1:])
            if l[0] >= v:
                return (l[0], 0)
            else:
                return (v, i + 1)
                
    def _allocate(n, e, l, v):
        """
        Place n e's into l (replacing the original content), based on the
        contents of v (largest first). Assume all v's >= 0.
        """
        if n == 0:
            return l
        else
            idx = _largest_index(v)
            l[idx] = e
            v[idx] = -1  # This index is never to be picked again
            return _allocate(n - 1, e, l, v)
    
    def _min(a, b):
        return a if a < b else b

